package com.csii.pos.caacctewinfo;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.util.excel.ExcelUtils;
import com.csii.adhoc.controller.BaseController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.util.ConvertUtils;
import com.csii.pos.caacctewinfo.service.ICaAcctEwInfoService;
import com.csii.pos.caacctewinfo.vo.CaAcctEwInfoVO;
import com.csii.pos.query.ExcelTemplate;
import com.csii.pos.query.service.IExcelTemplateService;
import com.csii.result.APIResult;
import com.csii.result.PageVo;
import com.csii.result.TableHtmlBuilder;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

 /**
 * @Description: 账户预警查询
 * @Author: power-stdio
 * @Date:   2021-07-09
 * @Version: V1.0
 */

@RestController
@RequestMapping("/caAcctEwInfo")
@Slf4j
public class CaAcctEwInfoController extends BaseController<CaAcctEwInfo, ICaAcctEwInfoService>{
	@Autowired
	private ICaAcctEwInfoService caAcctEwInfoService;
	 @Autowired
	 private IExcelTemplateService excelTemplateService;
	 @Autowired
	 private JdbcTemplate jdbcTemplate;
	/**
	 * 分页列表查询
	 *
	 * @param caAcctEwInfoVO  参数
	 * @return 分页查询结果
	 */
	@OperationLog(value = "账户预警查询-分页列表查询")
	@PostMapping(value = "/list")
	public APIResult<?> queryPageList(@RequestBody CaAcctEwInfoVO caAcctEwInfoVO) {
		CaAcctEwInfo caAcctEwInfo  = ConvertUtils.convert(CaAcctEwInfo.class,caAcctEwInfoVO);
		QueryWrapper<CaAcctEwInfo> queryWrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(caAcctEwInfo.getEwDt())){
			queryWrapper.like("EW_DT",caAcctEwInfo.getEwDt());
		}
		if (!StringUtils.isEmpty(caAcctEwInfo.getBankEnFs())){
			queryWrapper.like("BANK_EN_FS",caAcctEwInfo.getBankEnFs());
		}
		Page<CaAcctEwInfo> page = new Page<>(caAcctEwInfoVO.getPageNo(), caAcctEwInfoVO.getPageSize());
		IPage<CaAcctEwInfo> pageList = caAcctEwInfoService.page(page, queryWrapper);
		String html = TableHtmlBuilder.buildTableHtml(pageList.getRecords());
		return  APIResult.successResult(PageVo.getPageVoFromIPage(pageList)).setTableHtml(html);
	}

	/**
	 *   添加
	 *
	 * @param caAcctEwInfoVO  参数
	 * @return 添加结果
	 */
	@OperationLog(value = "账户预警查询-添加")
	@PostMapping(value = "/add")
	public APIResult<?> add(@RequestBody CaAcctEwInfoVO caAcctEwInfoVO) {
        caAcctEwInfoVO.setFiBcpNo("1");
        caAcctEwInfoVO.setFiNo("1");
		CaAcctEwInfo caAcctEwInfo  = ConvertUtils.convert(CaAcctEwInfo.class,caAcctEwInfoVO);
		caAcctEwInfoService.save(caAcctEwInfo);
		return APIResult.successResult("添加成功！");

	}

	/**
	 *  编辑
	 *
	 * @param caAcctEwInfoVO  参数
	 * @return 编辑结果
	 */
	@OperationLog(value = "账户预警查询-编辑")
	@PostMapping(value = "/edit")
	public APIResult<?> edit(@RequestBody CaAcctEwInfoVO caAcctEwInfoVO) {
		CaAcctEwInfo caAcctEwInfo  = ConvertUtils.convert(CaAcctEwInfo.class,caAcctEwInfoVO);
		caAcctEwInfoService.updateById(caAcctEwInfo);
		return APIResult.successResult("编辑成功!");
	}

	 /**
	  * @description:  输入框查询是提示接口
	  * @param: caAcctEwInfoVO
	  * @return:  模糊查询list
	  * @author liuzhonghua
	  * @date:
	  */
	 @RequestMapping("queryHint")
	 public APIResult<?> queryHint(@RequestBody CaAcctEwInfoVO caAcctEwInfoVO) {
		 QueryWrapper<CaAcctEwInfo> queryWrapper = new QueryWrapper<>();
		 if (!StringUtils.isEmpty(caAcctEwInfoVO.getBankEnFs())) {
			 queryWrapper.like("BANK_EN_FS", caAcctEwInfoVO.getBankEnFs());
		 }
		 if (!StringUtils.isEmpty(caAcctEwInfoVO.getEwDt())) {
			 queryWrapper.eq("EW_DT", caAcctEwInfoVO.getEwDt());
		 }
		 queryWrapper.select("DISTINCT BANK_EN_FS");
		 List<CaAcctEwInfo> list = caAcctEwInfoService.list(queryWrapper);
		 return APIResult.successResult(list);
	 }
	/**
	 *   通过id删除
	 * @param map  id
	 * @return  删除结果
	 */
	@OperationLog(value = "账户预警查询-通过ewTranSn删除")
	@PostMapping(value = "/delete")
	public APIResult<?> delete(@RequestBody Map<String,String> map) {
		caAcctEwInfoService.removeById(map.get("ewTranSn"));
		return APIResult.successResult("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids id集合
	 * @return 删除结果
	 */
//	@OperationLog(value = "账户预警查询-批量删除")
//	@PostMapping(value = "/deleteBatch")
//	public APIResult<?> deleteBatch(@RequestBody List<CaAcctEwInfoVO> ids) {
//		this.caAcctEwInfoService.removeByIds(ids.stream().map(CaAcctEwInfoVO::getId).collect(Collectors.toList()));
//		return APIResult.successResult("批量删除成功!");
//	}

	/**
	 * 通过id查询
	 *
	 * @param id id
	 * @return 查询结果
	 */
	@OperationLog(value = "账户预警查询-通过id查询")
	@PostMapping(value = "/queryById")
	public APIResult<?> queryById(@RequestParam(name="id") String id) {
		CaAcctEwInfo caAcctEwInfo = caAcctEwInfoService.getById(id);
		if(caAcctEwInfo==null) {
			return APIResult.failResult(null, "未找到对应数据");
		}
		CaAcctEwInfoVO caAcctEwInfoVO  = ConvertUtils.convert(CaAcctEwInfoVO.class,caAcctEwInfo);
		return APIResult.successResult(caAcctEwInfoVO);
	}

    /**
    * 导出excel
    *
    * @param caAcctEwInfoVO 参数
    */
    @RequestMapping(value = "/exportXls")
    public void exportXls(HttpServletResponse response, CaAcctEwInfoVO caAcctEwInfoVO) {
		QueryWrapper<ExcelTemplate> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("TABLE_NAME","CA_ACCT_EW_INFO");
		ExcelTemplate template = excelTemplateService.getOne(queryWrapper);
		ExcelUtils.exportTemplate(template.getTemplateName(), template.getTemplateDir(), jdbcTemplate, response);
    }

    /**
      * 通过excel导入数据
    *
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public APIResult<?> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        return APIResult.successResult(super.importExcel(file, CaAcctEwInfo.class));
    }
}
